﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using DAO;
using BUS;
public partial class Admin_Pages_WRENTRYManager : BasePage
{
    public static bool Insert;
    BUS_WRENTRY busWRENTRY = new BUS_WRENTRY();
    BUS_WRPRICETYPE busWrpricetype = new BUS_WRPRICETYPE();
    BUS_WRURGENT busWrurgent = new BUS_WRURGENT();
    BUS_WRCATEGORYLV1 busWrcategorylv1 = new BUS_WRCATEGORYLV1();
    BUS_WRENTTYPE busWrenttype = new BUS_WRENTTYPE();
    BUS_WRENTSALETYPE busWrentsaletype = new BUS_WRENTSALETYPE();
    BUS_WRUSER busWruser = new BUS_WRUSER();
    BUS_WRCITY busWrcity = new BUS_WRCITY();

    BUS_WRMEDIASTORE busWrmediastore = new BUS_WRMEDIASTORE();
    private DataTable dtMediastore = null;
    private void Show(string Mess)
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + Mess + "')", true);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Page.MaintainScrollPositionOnPostBack = true;
            pnUpdate.Visible = false;
            pnShow.Visible = true;
            BindGrid();
            BindDrop();
        }
    }
    private void BindGrid()
    {
        grvWRENTRY.DataSource = busWRENTRY.Get_Entry_List();
        grvWRENTRY.DataBind();
    }

    public void BindDrop()
    {
        string lang = Request.Cookies["SelLang"].Value;
        if (lang == "vi")
        {
            dropIDPriceType.DataSource =
               busWrpricetype._GetList()
                    .Where(q => q.Active == "Y" && q.IDLang == 2)
                    .Select(p => new { p.ID, p.PriceTypeName })
                    .ToList();
            dropIDPriceType.DataTextField = "PriceTypeName";
            dropIDPriceType.DataValueField = "ID";
            dropIDPriceType.DataBind();
        }
        else
        {
            dropIDPriceType.DataSource = busWrpricetype._GetList().Where(q => q.Active == "Y" && q.IDLang == 1).Select(p => new { p.ID, p.PriceTypeName }).ToList();
            dropIDPriceType.DataTextField = "PriceTypeName";
            dropIDPriceType.DataValueField = "ID";
            dropIDPriceType.DataBind();
        }
        if (lang == "vi")
        {
            dropIDUrgent.DataSource =
                busWrurgent._GetList()
                    .Where(q => q.Active == "Y" && q.IDLang == 2)
                    .Select(p => new { p.ID, p.UrgentName })
                    .ToList();
            dropIDUrgent.DataTextField = "UrgentName";
            dropIDUrgent.DataValueField = "ID";
            dropIDUrgent.DataBind();
        }
        else
        {
            dropIDUrgent.DataSource = busWrurgent._GetList().Where(q => q.Active == "Y" && q.IDLang == 1).Select(p => new { p.ID, p.UrgentName }).ToList();
            dropIDUrgent.DataTextField = "UrgentName";
            dropIDUrgent.DataValueField = "ID";
            dropIDUrgent.DataBind();
        }
        dropIDCategoryLv.DataSource = busWrcategorylv1._GetList().Where(q => q.Active == "Y").Select(p => new { p.ID, p.CategoryLv1Name }).ToList();
        dropIDCategoryLv.DataTextField = "CategoryLv1Name";
        dropIDCategoryLv.DataValueField = "Id";
        dropIDCategoryLv.DataBind();
        if (lang == "vi")
        {
            dropIDEntType.DataSource =
                busWrenttype._GetList()
                    .Where(q => q.Active == "Y" && q.IDLang == 2)
                    .Select(p => new { p.ID, p.EntryTypeName })
                    .ToList();
            dropIDEntType.DataTextField = "EntryTypeName";
            dropIDEntType.DataValueField = "ID";
            dropIDEntType.DataBind();
        }
        else
        {
            dropIDEntType.DataSource = busWrenttype._GetList().Where(q => q.Active == "Y" && q.IDLang == 1).Select(p => new { p.ID, p.EntryTypeName }).ToList();
            dropIDEntType.DataTextField = "EntryTypeName";
            dropIDEntType.DataValueField = "ID";
            dropIDEntType.DataBind();
        }
        if (lang == "vi")
        {
            dropIDSaleType.DataSource =
               busWrentsaletype._GetList()
                    .Where(q => q.Active == "Y" && q.IDLang == 2)
                    .Select(p => new { p.ID, p.TypeSaleName })
                    .ToList();
            dropIDSaleType.DataTextField = "TypeSaleName";
            dropIDSaleType.DataValueField = "ID";
            dropIDSaleType.DataBind();
        }
        else
        {
            dropIDSaleType.DataSource = busWrentsaletype._GetList().Where(q => q.Active == "Y" && q.IDLang == 1).Select(p => new { p.ID, p.TypeSaleName }).ToList();
            dropIDSaleType.DataTextField = "TypeSaleName";
            dropIDSaleType.DataValueField = "ID";
            dropIDSaleType.DataBind();
        }
        dropIDUserApprove.DataSource = busWruser._GetList().Where(q => q.Active == "Y" && q.ID == Int32.Parse(Session["userid"].ToString())).Select(p => new { p.ID, Name = p.Lastname + " " + p.Firstname }).ToList();
        dropIDUserApprove.DataTextField = "Name";
        dropIDUserApprove.DataValueField = "Id";
        dropIDUserApprove.DataBind();
        dropIDCity.DataSource = busWrcity._GetList().Where(q => q.Active == "Y").Select(p => new { p.ID, p.CityName }).ToList();
        dropIDCity.DataTextField = "CityName";
        dropIDCity.DataValueField = "Id";
        dropIDCity.DataBind();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        pnUpdate.Visible = true;
        pnShow.Visible = false;
        Insert = true;
        txtID.Text = "";
        txtEntryName.Text = "";
        txtEntContent.Text = "";
        txtViewed.Text = "";
        txtPrice.Text = "";
        txtPriceDesc.Text = "";
        txtNameToContact.Text = "";
        txtPhoneToContact.Text = "";
        txtAddressToContact.Text = "";
        txtEmailToContact.Text = "";
        txtUrgenFromDate.Text = "";
        txtUrgentToDate.Text = "";
        txtUrgentStatus.Text = "";
        txtKeywords.Text = "";


    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (grvWRENTRY.Items.Count > 0)
        {
            for (int i = 0; i < grvWRENTRY.Items.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)grvWRENTRY.Items[i].FindControl("chkSelect");
                if (chkSelect.Checked)
                {
                    string Id = grvWRENTRY.Items[i].Cells[1].Text;
                    try
                    {
                        busWRENTRY._Delete(Convert.ToInt32(Id));
                    }
                    catch
                    {
                    }
                }
            }
        }
        BindGrid();
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        if (txtEntryName.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập EntryName!");
            txtEntryName.Focus();
            return;
        }
        if (txtEntContent.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập EntContent!");
            txtEntContent.Focus();
            return;
        }
        if (txtPrice.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập Price!");
            txtPrice.Focus();
            return;
        }
        if (txtNameToContact.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập NameToContact!");
            txtNameToContact.Focus();
            return;
        }
        if (txtPhoneToContact.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập PhoneToContact!");
            txtPhoneToContact.Focus();
            return;
        }
        if (txtAddressToContact.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập AddressToContact!");
            txtAddressToContact.Focus();
            return;
        }
        if (txtEmailToContact.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập EmailToContact!");
            txtEmailToContact.Focus();
            return;
        }


        WRENTRY obj = new WRENTRY();

        obj.EntryName = txtEntryName.Text;
        obj.EntContent = txtEntContent.Text;
        obj.Viewed = Convert.ToInt32(txtViewed.Text);
        obj.Price = Convert.ToDecimal(txtPrice.Text);
        obj.PriceDesc = txtPriceDesc.Text;
        obj.IDPriceType = Convert.ToInt32(dropIDPriceType.SelectedValue);
        obj.NameToContact = txtNameToContact.Text;
        obj.PhoneToContact = txtPhoneToContact.Text;
        obj.AddressToContact = txtAddressToContact.Text;
        obj.EmailToContact = txtEmailToContact.Text;
        obj.IDUrgent = Convert.ToInt32(dropIDUrgent.SelectedValue);
        obj.UrgenFromDate = Common.Common.ConvertDateTime(txtUrgenFromDate.Text) == "" ? (DateTime?)null : Convert.ToDateTime(txtUrgenFromDate.Text);
        obj.UrgentToDate = Common.Common.ConvertDateTime(txtUrgentToDate.Text) == "" ? (DateTime?)null : Convert.ToDateTime(txtUrgentToDate.Text);
        obj.UrgentStatus = txtUrgentStatus.Text;
        obj.IDCategoryLv = Convert.ToInt32(dropIDCategoryLv.SelectedValue);
        obj.IDEntType = Convert.ToInt32(dropIDEntType.SelectedValue);
        obj.IDSaleType = Convert.ToInt32(dropIDSaleType.SelectedValue);
        obj.IDUserApprove = Convert.ToInt32(dropIDUserApprove.SelectedValue);
        obj.IDCity = Convert.ToInt32(dropIDCity.SelectedValue);
        obj.Active = dropActive.SelectedValue;
        obj.Keywords = txtKeywords.Text;
        obj.UpdateDate = DateTime.Now;
        obj.IDUpdateBy = Convert.ToInt32(Session["userid"].ToString());

        if (Insert)
        {
            obj.IDCreateBy = Convert.ToInt32(Session["userid"].ToString());
            obj.CreateDate = DateTime.Now;
            busWRENTRY._Add(obj);
        }
        else
        {
            obj.ID = Convert.ToInt32(txtID.Text);
            busWRENTRY._Update(obj);
            dtMediastore = Common.Common.ConvertToDataTable(busWrmediastore._GetList().Where(p => p.IDEntry == Int32.Parse(txtID.Text)).Select(q => new
            {
                q.MediaPath,
                q.ID

            }).ToList());
            if (dtMediastore != null && dtMediastore.Rows.Count > 0)
            {
                if (dtMediastore.Rows.Count == 4)
                {
                    busWrmediastore._Update(Int32.Parse(dtMediastore.Rows[0]["Id"].ToString()),
                        chkImg0.Checked == true ? "Y" : "N");
                    busWrmediastore._Update(Int32.Parse(dtMediastore.Rows[1]["Id"].ToString()),
                        chkImg1.Checked == true ? "Y" : "N");
                    busWrmediastore._Update(Int32.Parse(dtMediastore.Rows[2]["Id"].ToString()),
                        chkImg2.Checked == true ? "Y" : "N");
                    busWrmediastore._Update(Int32.Parse(dtMediastore.Rows[3]["Id"].ToString()),
                        chkImg3.Checked == true ? "Y" : "N");
                }
                else if (dtMediastore.Rows.Count == 3)
                {
                    busWrmediastore._Update(Int32.Parse(dtMediastore.Rows[0]["Id"].ToString()),
                        chkImg0.Checked == true ? "Y" : "N");
                    busWrmediastore._Update(Int32.Parse(dtMediastore.Rows[1]["Id"].ToString()),
                        chkImg1.Checked == true ? "Y" : "N");
                    busWrmediastore._Update(Int32.Parse(dtMediastore.Rows[2]["Id"].ToString()),
                        chkImg2.Checked == true ? "Y" : "N");
                }
                else if (dtMediastore.Rows.Count == 2)
                {
                    busWrmediastore._Update(Int32.Parse(dtMediastore.Rows[0]["Id"].ToString()),
                        chkImg0.Checked == true ? "Y" : "N");
                    busWrmediastore._Update(Int32.Parse(dtMediastore.Rows[1]["Id"].ToString()),
                        chkImg1.Checked == true ? "Y" : "N");
                }
                else if (dtMediastore.Rows.Count == 1)
                {
                    busWrmediastore._Update(Int32.Parse(dtMediastore.Rows[0]["Id"].ToString()),
                        chkImg0.Checked == true ? "Y" : "N");
                }
            }
        }
        BindGrid();
        pnUpdate.Visible = false;
        pnShow.Visible = true;
        Insert = false;

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        pnUpdate.Visible = false;
        pnShow.Visible = true;

    }

    protected void grvWRENTRY_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        string Id = e.CommandArgument.ToString();
        if (e.CommandName == "Edit")
        {
            Insert = false;
            WRENTRY _WRENTRY = busWRENTRY._GetObject(Convert.ToInt32(Id));
            txtID.Text = Id;
            txtEntryName.Text = _WRENTRY.EntryName;
            txtEntContent.Text = _WRENTRY.EntContent;
            txtViewed.Text = _WRENTRY.Viewed.ToString();
            txtPrice.Text = _WRENTRY.Price.ToString();
            txtPriceDesc.Text = _WRENTRY.PriceDesc;
            try
            {
                dropIDPriceType.SelectedValue = _WRENTRY.IDPriceType.ToString();
            }
            catch
            {
                Show("Price Type is not active!");
            }

            txtNameToContact.Text = _WRENTRY.NameToContact;
            txtPhoneToContact.Text = _WRENTRY.PhoneToContact;
            txtAddressToContact.Text = _WRENTRY.AddressToContact;
            txtEmailToContact.Text = _WRENTRY.EmailToContact;
            try
            {
                dropIDUrgent.SelectedValue = _WRENTRY.IDUrgent.ToString();
            }
            catch
            {
                Show("Urgent is not active!");
            }

            txtUrgenFromDate.Text = _WRENTRY.UrgenFromDate.ToString() == "" ? "" : Convert.ToDateTime(_WRENTRY.UrgenFromDate.ToString()).ToShortDateString();
            txtUrgentToDate.Text = _WRENTRY.UrgentToDate.ToString() == "" ? "" : Convert.ToDateTime(_WRENTRY.UrgentToDate.ToString()).ToShortDateString();
            txtUrgentStatus.Text = _WRENTRY.UrgentStatus;
            try
            {
                dropIDCategoryLv.SelectedValue = _WRENTRY.IDCategoryLv.ToString();
            }
            catch
            {
                Show("Category Lv is not active!");
            }
            try
            {
                dropIDEntType.SelectedValue = _WRENTRY.IDEntType.ToString();
            }
            catch
            {
                Show("Entry type is not active!");
            }
            try
            {
                dropIDSaleType.SelectedValue = _WRENTRY.IDSaleType.ToString();
            }
            catch
            {
                Show("Sale type is not active!");
            }
            try
            {
                dropIDUserApprove.SelectedValue = Session["userid"].ToString();
            }
            catch
            {
                Show("User Approve is not active!");
            }
            try
            {
                dropIDCity.SelectedValue = _WRENTRY.IDCity.ToString();
            }
            catch
            {
                Show("City is not active!");
            }
            dropActive.SelectedValue = _WRENTRY.Active.ToString();
            txtKeywords.Text = _WRENTRY.Keywords;

            dtMediastore = Common.Common.ConvertToDataTable(busWrmediastore._GetList().Where(p => p.IDEntry == Int32.Parse(Id)).Select(q => new
            {
                q.MediaPath,
                q.ID,
                q.Active

            }).ToList());
            if (dtMediastore != null && dtMediastore.Rows.Count > 0)
            {
                if (dtMediastore.Rows.Count == 4)
                {
                    img0.ImageUrl = dtMediastore.Rows[0]["MediaPath"].ToString();
                    img1.ImageUrl = dtMediastore.Rows[1]["MediaPath"].ToString();
                    img2.ImageUrl = dtMediastore.Rows[2]["MediaPath"].ToString();
                    img3.ImageUrl = dtMediastore.Rows[3]["MediaPath"].ToString();
                    chkImg0.Checked = dtMediastore.Rows[0]["Active"].ToString() != "N";
                    chkImg1.Checked = dtMediastore.Rows[1]["Active"].ToString() != "N";
                    chkImg2.Checked = dtMediastore.Rows[2]["Active"].ToString() != "N";
                    chkImg3.Checked = dtMediastore.Rows[3]["Active"].ToString() != "N";
                }
                else if (dtMediastore.Rows.Count == 3)
                {
                    img0.ImageUrl = dtMediastore.Rows[0]["MediaPath"].ToString();
                    img1.ImageUrl = dtMediastore.Rows[1]["MediaPath"].ToString();
                    img2.ImageUrl = dtMediastore.Rows[2]["MediaPath"].ToString();
                    chkImg0.Checked = dtMediastore.Rows[0]["Active"].ToString() != "N";
                    chkImg1.Checked = dtMediastore.Rows[1]["Active"].ToString() != "N";
                    chkImg2.Checked = dtMediastore.Rows[2]["Active"].ToString() != "N";
                    img3.Visible = false;
                    chkImg3.Visible = false;
                }
                else if (dtMediastore.Rows.Count == 2)
                {
                    img0.ImageUrl = dtMediastore.Rows[0]["MediaPath"].ToString();
                    img1.ImageUrl = dtMediastore.Rows[1]["MediaPath"].ToString();
                    chkImg0.Checked = dtMediastore.Rows[0]["Active"].ToString() != "N";
                    chkImg1.Checked = dtMediastore.Rows[1]["Active"].ToString() != "N";
                    img2.Visible = false;
                    img3.Visible = false;
                    chkImg2.Visible = false;
                    chkImg3.Visible = false;
                }
                else if (dtMediastore.Rows.Count == 1)
                {
                    img0.ImageUrl = dtMediastore.Rows[0]["MediaPath"].ToString();
                    chkImg0.Checked = dtMediastore.Rows[0]["Active"].ToString() != "N";
                    img2.Visible = false;
                    img3.Visible = false;
                    img1.Visible = false;
                    chkImg1.Visible = false;
                    chkImg2.Visible = false;
                    chkImg3.Visible = false;
                }
                else
                {
                    img2.Visible = false;
                    img3.Visible = false;
                    img1.Visible = false;
                    img0.Visible = false;
                    chkImg0.Visible = false;
                    chkImg1.Visible = false;
                    chkImg2.Visible = false;
                    chkImg3.Visible = false;
                }
            }

            pnUpdate.Visible = true;
            pnShow.Visible = false;
        }
        else if (e.CommandName == "Delete")
        {
            busWRENTRY._Delete(Convert.ToInt32(Id));
            BindGrid();
        }

    }

    protected void grvWRENTRY_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        grvWRENTRY.CurrentPageIndex = e.NewPageIndex;
        BindGrid();
    }
    protected void ibtCal_Click(object sender, ImageClickEventArgs e)
    {

    }
    protected void ibtUrgentToDate_Click(object sender, ImageClickEventArgs e)
    {

    }
}
